package com.service.Impl;

import com.mapper.CTicketMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CTicket;
import com.pojo.vo.CTicketRoundVo;
import com.pojo.vo.CTicketVo;
import com.service.CTicketRoundService;
import com.service.CTicketService;
import com.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("CTickService")
public class CTickServiceImpl implements CTicketService {
    @Autowired
    CTicketMapper cTicketMapper;
    @Autowired
    CTicketRoundService cTicketRoundService;

    @Override
    public Message<CTicketVo> add(CTicketVo cTicketVo) {
        CTicketRoundVo cTicketRoundVo = cTicketRoundService.selectOne(cTicketVo.getPlanId()).getData();
        if (cTicketRoundVo == null) {
            return Message.errorMessage("1001", "plan_id不存在");
        }
        List<CTicketVo> data = list(cTicketVo).getData();
        if (data.isEmpty()) {
            cTicketVo.setChargeTime(new Date());
            cTicketVo.setState(1L);
            int i = cTicketMapper.insert(PojoUtil.copyObject(cTicketVo, CTicket.class));
            if (i == 0) return Message.errorMessage("1001", "添加失败");
            cTicketRoundVo.setUserCount(cTicketRoundVo.getUserCount() + 1);
            cTicketRoundService.update(cTicketRoundVo);
            return Message.successMessage(1L, cTicketVo);
        }
        return Message.successMessage(1L, null);
    }

    @Override
    public Message<CTicketVo> delete(Long id) {

        CTicketVo cTicketVo = new CTicketVo();
        cTicketVo.setId(id);
        CTicketVo cTicketVo1 = selectOne(cTicketVo).getData();
        CTicketRoundVo cTicketRoundVo = cTicketRoundService.selectOne(cTicketVo1.getPlanId()).getData();
        cTicketRoundVo.setUserCount(cTicketRoundVo.getUserCount() - 1);
        cTicketRoundService.update(cTicketRoundVo);
        int i = cTicketMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        return Message.successMessage(1L, null);
    }

    @Override
    public Message<CTicketVo> update(CTicketVo cTicketVo) {
        int i = cTicketMapper.updateById(PojoUtil.copyObject(cTicketVo, CTicket.class));
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        return Message.successMessage(1L, cTicketVo);
    }

    @Override
    public Message<List<CTicketVo>> list(CTicketVo cTicketVo) {
        QueryWrapper<CTicket> queryWrapper = new QueryWrapper<>();
        if (cTicketVo != null) {
            if (!StringUtils.isEmpty(cTicketVo.getId())) {
                queryWrapper.eq("id", cTicketVo.getId());
            }
            if (!StringUtils.isEmpty(cTicketVo.getPlanId())) {
                queryWrapper.eq("plan_id", cTicketVo.getPlanId());
            }
            if (!StringUtils.isEmpty(cTicketVo.getUserNum())) {
                queryWrapper.eq("user_num", cTicketVo.getUserNum());
            }
            if (!StringUtils.isEmpty(cTicketVo.getWeixin())) {
                queryWrapper.eq("weixin", cTicketVo.getWeixin());
            }
            if (!StringUtils.isEmpty(cTicketVo.getState())) {
                queryWrapper.eq("state", cTicketVo.getState());
            }
        }
        List<CTicket> cTickets = cTicketMapper.selectList(queryWrapper);
        List<CTicketVo> list = new ArrayList<>();
        for (int i = 0; i < cTickets.size(); i++) {
            list.add(PojoUtil.copyObject(cTickets.get(i), CTicketVo.class));
        }
        return Message.successMessage(1L, list);
    }

    @Override
    public Message<CTicketVo> selectOne(CTicketVo cTicketVo) {
        QueryWrapper<CTicket> queryWrapper = new QueryWrapper<>();
        if (cTicketVo != null) {
            if (!StringUtils.isEmpty(cTicketVo.getId())) {
                queryWrapper.eq("id", cTicketVo.getId());
            }
            if (!StringUtils.isEmpty(cTicketVo.getPlanId())) {
                queryWrapper.eq("plan_id", cTicketVo.getPlanId());
            }
            if (!StringUtils.isEmpty(cTicketVo.getUserNum())) {
                queryWrapper.eq("user_num", cTicketVo.getUserNum());
            }
        }
        CTicket cTicket = cTicketMapper.selectOne(queryWrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(cTicket, CTicketVo.class));
    }

}
